<?php
/**
 * 后台用户管理
 * User: Richard
 * Date: 2020/3/15
 * Time: 12:31
 */

namespace Demo\Controller;


use Common\Controller\DemoBaseController;

class UserController extends DemoBaseController

{
    private $userModel;

    function __construct()
    {
        parent::__construct();
        $this->userModel = M('users');
    }

    /**
     * 后台用户管理
     */
    public function index()
    {
        $pageCount = I('count', 20, 'intval');//一页20个

        $where = [];
        $username = I('username');
        if (!empty($username)) {
            $where['username'] = ['like', '%' . $username . '%'];
        }
        $count = $this->userModel->where($where)->count();

        $page = $this->page($count, $pageCount);

        $list = $this->userModel->where($where)
            ->order('create_time DESC')
            ->limit($page->firstRow, $page->listRows)
            ->select();
        $this->returnok(['list' => $list, 'page' => $page]);
    }

    /**
     * 添加用户提交
     */
    public function add_post()
    {
        $username = I('username');
        if (empty($username)) {
            $this->returnerror('', '请输入账号');
        }
        $password = I('password');
        if (empty($username)) {
            $this->returnerror('', '请输入密码');
        }
        $nickname = I('nickname');
        if (empty($nickname)) {
            $this->returnerror('', '请输入昵称');
        }
        $roleId = I('role_id', 0, 'intval');
        if (empty($roleId)) {
            $this->returnerror('', '请选择角色');
        }
        $hasUser = $this->userModel->where(['username' => $username])->count() ? true : false;
        if ($hasUser) {
            $this->returnerror('', '用户已存在');
        }
        $data = [
            'username' => $username,
            'password' => $password,
            'nickname' => $nickname,
            'role_id' => $roleId,
        ];
        $id = $this->userModel->add($data);
        if (empty($id)) {
            $this->returnerror('', '添加失败，请重试');
        } else {
            $this->returnok(['id' => $id], '添加成功');
        }
    }

    /**
     * 编辑用户
     */
    public function edit()
    {
        $userId = I('id', 0, 'intval');
        if (empty($userId)) {
            $this->returnerror('', '无效请求');
        }
        $userInfo = $this->userModel->where(['id' => $userId])->field('id,username,nickname')->find();
        if (empty($userInfo)) {
            $this->returnerror('', '未找到指定用户');
        }
        $roleInfo = M('role')->where(['status' => 1])->field('id,name')->select();
        $this->returnok(['userInfo' => $userInfo, 'roleInfo' => $roleInfo]);
    }

    /**
     * 编辑用户提交
     */
    public function edit_post()
    {
        $userId = I('id', 0, 'intval');
        if (empty($userId)) {
            $this->returnerror('', '无效请求');
        }
        $username = I('username');
        if (empty($username)) {
            $this->returnerror('', '请输入账号');
        }
        $password = I('password');
        $nickname = I('nickname');
        if (empty($nickname)) {
            $this->returnerror('', '请输入昵称');
        }
        $roleId = I('role_id', 0, 'intval');
        if (empty($roleId)) {
            $this->returnerror('', '请选择角色');
        }
        $hasUser = $this->userModel->where(['username' => $username, 'id' => ['neq', $userId]])->count() ? true : false;
        if ($hasUser) {
            $this->returnerror('', '用户已存在');
        }
        $data = [
            'username' => $username,
            'nickname' => $nickname,
            'role_id' => $roleId,
        ];
        if (!empty($password)) {
            $data['password'] = $password;
        }
        $res = $this->userModel->where(['id' => $userId])->save($data);
        if ($res !== false || $res === 0) {
            $this->returnok(['id' => $userId], '修改成功');
        } else {
            $this->returnerror('', '修改失败，请重试');
        }

    }

    /**
     * 拉黑/恢复用户
     */
    public function action()
    {
        $userId = I('id', 0, 'intval');
        if (empty($userId)) {
            $this->returnerror('', '无效请求');
        }
        $status = I('status', 0, 'intval');
        $res = $this->userModel->where(['id' => $userId])->save(['status' => $status]);
        if ($res !== false) {
            $this->returnok(['id' => $userId], '操作成功');
        } else {
            $this->returnerror('', '操作失败，请重试');
        }
    }
}